package com.test.rest.client;

import java.io.File;
import java.io.InputStream;
import java.net.URL;

import org.apache.cxf.helpers.IOUtils;
import org.apache.cxf.io.CachedOutputStream;
import org.apache.cxf.resource.URIResolver;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.FileEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class Client {

	public static void main(String[] args) throws Exception {

		URL url = new URL("http://localhost:9000/customerservice/customers/123");

		InputStream in = url.openStream();

		System.out.println(getStringFromInputStream(in));

		System.out.println("----------------------------------------");

		url = new URL("http://localhost:9000/customerservice/orders/223/products/323");

		in = url.openStream();

		System.out.println(getStringFromInputStream(in));

		System.out.println("----------------------------------------");

		Client client = new Client();
		String inputFile = client.getClass().getResource("update_customer.xml").getFile();
		URIResolver resolver = new URIResolver(inputFile);
		File input = new File(resolver.getURI());

		HttpPut put = new HttpPut("http://localhost:9000/customerservice/customers");
		HttpEntity entity = new FileEntity(input, ContentType.APPLICATION_XML);
		put.setEntity(entity);

		HttpClient httpClient = new DefaultHttpClient();

		try {

			HttpResponse response = httpClient.execute(put);

			System.out.println("code : " + response.getStatusLine().getStatusCode());
			System.out.println(EntityUtils.toString(response.getEntity()));
		} finally {
			put.releaseConnection();
		}

		System.out.println("----------------------------------------");

		inputFile = client.getClass().getResource("add_customer.xml").getFile();
		resolver = new URIResolver(inputFile);
		input = new File(resolver.getURI());

		HttpPost post = new HttpPost("http://localhost:9000/customerservice/customers");
//		post.setHeader("Accept", "text/xml");
		post.setHeader("Accept", "application/json");

		entity = new FileEntity(input, ContentType.APPLICATION_XML);
		post.setEntity(entity);

		try {

			HttpResponse response = httpClient.execute(post);

			System.out.println("code : " + response.getStatusLine().getStatusCode());
			System.out.println(EntityUtils.toString(response.getEntity()));
		} finally {
			post.releaseConnection();
		}
		
		System.out.println("----------------------------------------");
		
		HttpDelete delete = new HttpDelete("http://localhost:9000/customerservice/customers/123");

		httpClient = new DefaultHttpClient();
		
		try {

			HttpResponse response = httpClient.execute(delete);

			System.out.println("code : " + response.getStatusLine().getStatusCode());
			System.out.println(EntityUtils.toString(response.getEntity()));
		} finally {
			delete.releaseConnection();
		}
		
	}

	private static String getStringFromInputStream(InputStream in) throws Exception {

		CachedOutputStream cos = new CachedOutputStream();

		IOUtils.copy(in, cos);

		in.close();
		cos.close();

		return cos.getOut().toString();
	}

}
